﻿namespace BM.Tools.WebControls.MForm.Controls.ValueHandlers
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI.WebControls;

    /// <summary>
    /// A helper class for list control value handlers that may have more than one selected item.
    /// </summary>
    public class MultipleListControlValueHandlerHelper
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MultipleListControlValueHandlerHelper"/> class.
        /// </summary>
        /// <param name="handler">
        /// The handler.
        /// </param>
        public MultipleListControlValueHandlerHelper(IMultipleListControlValueHandler handler)
        {
            Handler = handler;
        }

        /// <summary>
        /// Gets the value handler that this class serves.
        /// </summary>
        public IMultipleListControlValueHandler Handler
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets or sets the value of the list item control.
        /// </summary>
        public string Value
        {
            get
            {
                var selectedValues = new List<string>();
                foreach (ListItem item in Handler.ListControl.Items)
                {
                    if (item.Selected)
                    {
                        selectedValues.Add(item.Value);
                    }
                }

                return string.Join(Handler.ValueSeparator, selectedValues.ToArray());
            }

            set
            {
                ClearControlValue();
                foreach (var singleValue in GetValues(value))
                {
                    var item = Handler.ListControl.Items.FindByValue(singleValue);
                    if (item != null)
                    {
                        item.Selected = true;
                    }
                }
            }
        }

        /// <summary>
        /// Gets the control value for view mode
        /// </summary>        
        public string ViewModeControlValue
        {
            get
            {
                var selectedTexts = new List<string>();
                foreach (ListItem item in Handler.ListControl.Items)
                {
                    if (item.Selected)
                    {
                        selectedTexts.Add(item.Text);
                    }
                }

                return string.Join(Handler.ValueSeparator, selectedTexts.ToArray());
            }
        }

        private void ClearControlValue()
        {
            foreach (ListItem li in Handler.ListControl.Items)
            {
                li.Selected = false;
            }
        }

        private string[] GetValues(string value)
        {
            return value.Split(new[] { Handler.ValueSeparator }, StringSplitOptions.RemoveEmptyEntries);
        }        
    }
}
